import 'package:duqing/config/app_colors.dart';
import 'package:duqing/view/root_pages/home_page.dart';
import 'package:duqing/view/root_pages/music_page.dart';
import 'package:duqing/view/root_pages/profile.dart';
import 'package:duqing/view/root_pages/tiny_video.dart';
import 'package:flutter/material.dart';
import 'package:proste_indexed_stack/proste_indexed_stack.dart';

class RootPage extends StatefulWidget {
  const RootPage({Key key}) : super(key: key);

  @override
  _RootPageState createState() => _RootPageState();
}

const Map<String, String> _bottonNames = {
  'home': '首页',
  'music': '音乐',
  'create_media': '',
  'tiny_video': '小视频',
  'profile': '我的',
};

class _RootPageState extends State<RootPage> {
  int _currentIndex = 0;

  final List<Widget> _pages = [
    HomePage(),
    MusicPage(),
    Container(),
    TinyVideo(),
    ProfilePage(),
  ];

  //底部导航数组
  final List<BottomNavigationBarItem> _bottomNavBarList = [];

  @override
  void initState() {
    super.initState();
    _bottonNames.forEach((key, value) {
      _bottomNavBarList.add(_bootomNavBatItem(key, value));
    });
  }

  void _onTapClick(int index) {
    if (index == 2) return _onCreateMedia();
    setState(() {
      _currentIndex = index;
    });
  }

  void _onCreateMedia() {
    print("发布");
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: ProsteIndexedStack(
        index: _currentIndex,
        children: _pages,
      ),
      bottomNavigationBar: BottomNavigationBar(
        items: _bottomNavBarList,
        currentIndex: _currentIndex,
        onTap: _onTapClick,
        type: BottomNavigationBarType.fixed,
      ),
      floatingActionButton: _createMediaButton(),
      floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
    );
  }

  //发布按钮
  Widget _createMediaButton() {
    return Container(
      width: 44,
      height: 44,
      margin: EdgeInsets.only(top: 56),
      child: FloatingActionButton(
        backgroundColor: AppColors.nav,
        child: Image.asset(
          'assets/images/icons/create_media.png',
        ),
        onPressed: _onCreateMedia,
      ),
    );
  }

  BottomNavigationBarItem _bootomNavBatItem(String key, String value) {
    return BottomNavigationBarItem(
      icon: Image.asset(
        'assets/images/icons/$key.png',
        width: 24,
        height: 24,
      ),
      activeIcon: Image.asset(
        'assets/images/icons/${key}_active.png',
        width: 24,
        height: 24,
      ),
      label: value,
      tooltip: '',
    );
  }
}
